#include "Comida.h"

namespace gameCG {

	Comida::Comida() {
		srand(time(NULL));

		direction = rand()%4;
		counter = 0;
		limite = 50 + rand()%50;
		speed = 0.001f;

		int signalx = rand() % 2;
		signalx = (signalx)?1:-1;
		int signalz = rand() % 2;
		signalz = (signalz)?1:-1;
		z = (float) signalz*((rand()%450)/100.0f);
		x = (float) signalx*((rand()%450)/100.0f);
		Translate(x,0.0f,z);
	}

	float Comida::getX() {
		return x;
	}

	float Comida::getZ() {
		return z;
	}
	
	void Comida::draw() {
		glPushAttrib(GL_LIGHTING_BIT);
			GLfloat diffuse[] = {0.8f,0.2f,0.5f,1.0f};
			GLfloat specular[] = {1.0f,1.0f,1.0f,1.0f};
			GLfloat shininess[] = {50.0f};
			glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
			glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
			glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
			glutSolidSphere(0.4f, 20, 16);
		glPopAttrib();
	}

 	void Comida::update(int delta) {
		counter++;
		if(counter > limite) {
			changeDirection();
			counter = 0;
		}
			
		if(direction == 0) {
			x += speed*delta;
			if(x > 4.5) direction = 1;
			Translate(speed*delta,0.0f,0.0f);
		}
		else if(direction == 1) {
			x -= speed*delta;
			if(x < -4.5) direction = 0;
			Translate(-speed*delta,0.0f,0.0f);
		}
		else if(direction == 2) {
			z += speed*delta;
			if(z > 4.5) direction = 3;
			Translate(0.0f,0.0f,speed*delta);
		}
		else {
			z -= speed*delta;
			if(z < -4.5) direction = 2;
			Translate(0.0f,0.0f,-speed*delta);
		}
 	}

	void Comida::changeDirection() {
		direction = rand()%4;
	}

	void Comida::resetSpeed() {
		speed = 0.001f;
		direction = 0;	
	}

	void Comida::increaseSpeed() {
		speed += 0.001;
	}

	void Comida::consume() {
		int signalx = rand() % 2;
		signalx = (signalx)?1:-1;
		int signalz = rand() % 2;
		signalz = (signalz)?1:-1;
		z = (float) signalz*((rand()%500)/100.0f);
		x = (float) signalx*((rand()%500)/100.0f);
		clearTransformations();
		Translate(x,0.0f,z);
	}
}
